import json
from app_db import app
from src.model.base_opera import *
from src.sqoop import sshclient_exec


@app.route('/api/v0/dbinfo')
def db_info():
    r = read_dim_db()
    json_data = {
        'code': 200,
        'data': r
    }
    return json.dumps(json_data)


@app.route('/api/v0/tableinfo', methods=['POST'])
def api_table_info():
    db_id = request.json.get('db_id')
    if db_id and db_id != 'None':
        r = read_db_table_relate(db_id=int(db_id))
        json_data = {
            'code': 200,
            'data': r
        }
        print('json_data: {}'.format(json_data))
        return jsonify(json_data)


@app.route('/api/v0/columninfo', methods=['POST'])
def table_column_info():
    table_id = request.json.get('table_id')
    if table_id and table_id != 'None':
        r = read_table_column_relate(table_id=int(table_id))
        json_data = {
            'code': 200,
            'data': r
        }
        return jsonify(json_data)
    else:
        db_id = request.json.get('db_id')
        category_id = request.json.get('category_id')
        if db_id:
            query_info = DimColumn.query. \
                join(DimDataType, DimDataType.id == DimColumn.data_type_id). \
                join(DimDb, DimDb.db_driver_id == DimDataType.db_driver_id).\
                filter(DimDb.id == db_id).filter(DimColumn.is_valid == 1)

            if category_id and category_id >= 0:
                query_info = query_info.filter(DimColumn.category_id == category_id)
            table_info = query_info.all()
            if table_info:
                arr = [{
                    'id': data.id,
                    'column_name': data.column_name
                } for data in table_info]
            else:
                arr = []
            json_data = {
                'code': 200,
                'data': arr
            }
            return jsonify(json_data)


# 按 db_driver_id查询data_type
@app.route('/api/v0/data_type_info', methods=['POST'])
def api_data_type_info():
    db_driver_id = request.json.get('db_driver_id')
    if db_driver_id and db_driver_id != 'None':
        r = read_dim_data_type(db_driver_id=int(db_driver_id))
        if r:
            arr = [{
                'id': data.get('id'),
                'data_type_name': data.get('data_type_name')
            } for data in r]
            json_data = {
                'code': 200,
                'data': arr
            }
            return jsonify(json_data)


# sqoop 调度api
@app.route('/api/v0/sqoop_cmd')
def api_sqoop_cmd():
    id = int(request.args.get('id')) if request.args.get('id') else None
    env = int(request.args.get('env')) if request.args.get('env') else None
    if id:
        this_info = SqoopInfo.query.join(User, User.id == SqoopInfo.owner_id). \
            add_columns(SqoopInfo.source_db, SqoopInfo.source_table, SqoopInfo.target_info, SqoopInfo.sqoop_cmd,
                        User.name). \
            filter(SqoopInfo.id == id).first()
        sqoop_cmd = this_info.sqoop_cmd if this_info else None
        if sqoop_cmd:
            cmd = 'su - hive -c  "{}"'.format(sqoop_cmd)
            if env == 1:
                sshclient_exec(cmd=cmd, env='PDE')
            else:
                sshclient_exec(cmd=cmd)
            return 'ok', 200
    return 400, 'error'
